
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import math

# like chip: https://doc.qt.io/qt-5/qtwidgets-graphicsview-chip-example.html

# QLine
# QColor
# QPen

class NodeEditorScene(QGraphicsScene):
    def __init__(self, parent=None):
        super().__init__(parent)

        # setting
        self.gridSize = 20
        self.gridSquare = 5

        self._color_background = QColor("#393939")  # background color
        self._color_light = QColor("#2f2f2f")
        self._color_dark = QColor("#292929")

        self._pen_light = QPen(self._color_light) # Pen以颜色初始化
        self._pen_light.setWidth(1)  # Pen线宽
        self._pen_dark = QPen(self._color_dark)
        self._pen_dark.setWidth(2)

        self.scene_width, self.scene_height = 6400,6400  # size
        
        
        self.setBackgroundBrush(self._color_background)
        self.setSceneRect(-self.scene_width//2,-self.scene_height//2,
                        self.scene_width,self.scene_height)

    def drawBackground(self,painter,rect):
        super().drawBackground(painter,rect)

        # draw grid
        left = int(math.floor(rect.left()))
        right = int(math.ceil(rect.right()))
        top = int(math.floor(rect.top()))
        bottom = int(math.ceil(rect.bottom()))
        first_left = left - (left % self.gridSize)
        first_top = top - (top % self.gridSize)

        # compute all lines to be drawn
        lines_light = []
        lines_dark = [] 
        for i in range(first_left,right,self.gridSize):  # draw line left to right
            if (i % (self.gridSize * self.gridSquare) != 0):
                # 间隔gridSquare个小网格画浅色线
                lines_light.append((QLine(i,top,i,bottom)))
            else:
                lines_light.append((QLine(i,top,i,bottom)))
                #lines_dark.append((QLine(i,top,i,bottom)))
        for j in range(first_top,bottom,self.gridSize):  # draw line top to bottom
            if(j % (self.gridSize * self.gridSquare) != 0):
                lines_light.append((QLine(left,j,right,j)))
            else:
                lines_light.append((QLine(left,j,right,j)))
                #lines_dark.append((QLine(left,j,right,j)))

        # 间隔距离大的画深色线lines_dark，小距离画浅色线
        painter.setPen(self._pen_light)
        painter.drawLines(*lines_light) #?? *??
        #painter.setPen(self._pen_dark)
        #painter.drawLines(*lines_dark) #?? *??
